﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.JummahPrayers
{
    public static class CreateLocation
    {
        public static Adams.Admin.DataTransfer.JummahPrayers.CreateLocation.PageLoad PageLoad(int id)
        {
            var data = new Adams.Admin.DataTransfer.JummahPrayers.CreateLocation.PageLoad();
            data.Location = new Shared.Entities.JummahPrayers.Location();

            if (id > 0)
            {
                using (SqlDataReader dr = DB.RunDataReader(@"
SELECT * FROM JummahPrayers.Locations WHERE LocationID = @LocationID
", CommandType.Text,
                DB.CreateParameter("@LocationID", id, SqlDbType.Int)))
                {
                    dr.Read();
                    data.Location.ID = dr["LocationID"].DbInt();
                    data.Location.Name = dr["Name"].DbString();
                    data.Location.Street = dr["Street"].DbString();
                    data.Location.Street2 = dr["Street2"].DbString();
                    data.Location.City = dr["City"].DbString();
                    data.Location.State = dr["State"].DbString();
                    data.Location.ZipCode = dr["ZipCode"].DbString();
                    data.Location.Latitude = dr["Latitude"].DbDecimal();
                    data.Location.Longitude = dr["Longitude"].DbDecimal();
                    data.Location.FridayNote = dr["FridayNote"].DbString();
                    data.Location.LongNote = dr["LongNote"].DbString();
                    data.Location.DisplayOrder = dr["DisplayOrder"].DbInt();
                }
            }

            return data;
        }

        public static void Save(DataTransfer.JummahPrayers.CreateLocation.FormInput input)
        {
            DB.Run(@"
IF(@LocationID > 0)
BEGIN
    UPDATE JummahPrayers.Locations SET
        Name = @Name,
        Street = @Street,
        Street2 = @Street2,
        City = @City,
        State = @State,
        ZipCode = @ZipCode,
        Latitude = @Latitude,
        Longitude = @Longitude,
        FridayNote = @FridayNote,
        LongNote = @LongNote,
        DisplayOrder = @DisplayOrder
    WHERE LocationID = @LocationID
END
ELSE
BEGIN
    INSERT INTO JummahPrayers.Locations (
        Name,
        Street,
        Street2,
        City,
        State,
        ZipCode,
        Latitude,
        Longitude,
        FridayNote,
        LongNote,
        DisplayOrder)
    VALUES (
        @Name,
        @Street,
        @Street2,
        @City,
        @State,
        @ZipCode,
        @Latitude,
        @Longitude,
        @FridayNote,
        @LongNote,
        @DisplayOrder)
END
    ", CommandType.Text,
                    DB.CreateParameter("@LocationID", input.ID.ToInt(), SqlDbType.Int),
                    DB.CreateParameter("@Name", input.Name.ToNString(), SqlDbType.VarChar, 50),
                    DB.CreateParameter("@Street", input.Street.ToNString(), SqlDbType.VarChar, 100),
                    DB.CreateParameter("@Street2", input.Street2.ToNString(), SqlDbType.VarChar, 100),
                    DB.CreateParameter("@City", input.City.ToNString(), SqlDbType.VarChar, 50),
                    DB.CreateParameter("@State", input.State.ToNString(), SqlDbType.VarChar, 2),
                    DB.CreateParameter("@ZipCode", input.ZipCode.ToNString(), SqlDbType.VarChar, 10),
                    DB.CreateParameter("@Latitude", input.Latitude.ToNString(), SqlDbType.Decimal),
                    DB.CreateParameter("@Longitude", input.Longitude.ToNString(), SqlDbType.Decimal),
                    DB.CreateParameter("@FridayNote", input.FridayNote.ToNString(), SqlDbType.VarChar, 50),
                    DB.CreateParameter("@LongNote", input.LongNote.ToNString(), SqlDbType.Text),
                    DB.CreateParameter("@DisplayOrder", input.DisplayOrder.ToNString(), SqlDbType.Int));
        }
    }
}
